package com.djzhou.gmms.util.properties;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hp.hpl.sparta.xpath.ThisNodeTest;

import javax.servlet.http.HttpSession;
/**
 * 配置文件工具类
 * @（#）:PropUtil.java 
 * @author:  DJZHOU  2012-2-27 
 * @version: [SVN] 
 * @modify: 
 * @Copyright:  黑龙江中科方德软件有限公司
 */
public class PropUtil {
	/**
	 * 读取配置文件并从配置文件中返回用户传入的关键字对应的存储过程
	 * @param luceneProPath 文件路径（lucene.properties）
	 * @param key           关键字
	 * @return 返回NULL当前传入的关键字没有对应的存储过程
	 */
	public static String loadStatistic(String filename,String key){
		HttpServletRequest req = ServletActionContext.getRequest();
		String realPath = req.getSession().getServletContext().getRealPath("/");
		realPath+="WEB-INF/classes/"+filename+".properties";
		try {
			Properties props = new Properties();
			props.load(new BufferedInputStream(new FileInputStream(realPath)));
			return props.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		String attendanceDayStr=PropUtil.loadStatistic("config", "attendanceDay");
		System.out.println(attendanceDayStr);
	}
	
	public static String getPropertyValue(HttpSession session , String name,String fileName)
	{
		Properties prop = new Properties();
		String realPath = session.getServletContext().getRealPath("/");
		realPath+="WEB-INF/classes/"+fileName+".properties";
		try {
			prop.load(new BufferedInputStream(new FileInputStream(realPath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return prop.getProperty(name);
	}

}
